package com.yikome.shop.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;
import com.itmuch.lightsecurity.annotation.PreAuthorize;
import com.yikome.shop.dto.FeedBackSearchRequest;
import com.yikome.shop.dto.FeedBackSearchResponse;
import com.yikome.shop.entity.Navigation;
import com.yikome.shop.entity.NavigationCollection;
import com.yikome.shop.entity.Shop;
import com.yikome.shop.service.NavigationService;
import com.yikome.shop.service.ShopService;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 商城控制类,1.查询,2.新增一个商城,3.删除一个商城,4.修改一个商城
 */
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {

	@Autowired
	private ShopService shopService;
	
	@Autowired
	private NavigationService navigationService; 
	
	@GetMapping
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop', 'affiliate')")
	public List<Shop> findShop(){
		return shopService.findAll();
	}
	
	@GetMapping("/combobox")
	@PreAuthorize("hasLogin()")
	public List<Map<String , Object>> findShopCombo(){
		return shopService.findShopCombo();
	}
	
	@GetMapping("/host/combobox")
	@PreAuthorize("hasLogin()")
	public List<Map<String , Object>> findShopHostCombo(){
		return shopService.findShopHostCombo();
	}
	
	@PostMapping
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Shop presistShop(@RequestBody Shop shopRequest) {
		Preconditions.checkArgument(StrUtil.isNotBlank(shopRequest.getName()), "名称不能为空...");
		return shopService.presistShop(shopRequest);
	}
	
	@PutMapping("/logo")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Shop updateShopLogo(@RequestBody Shop shopRequest) {
		Preconditions.checkArgument(StrUtil.isNotBlank(shopRequest.getLogo()), "LOGO不能为空...");
		Preconditions.checkArgument(shopRequest.getId() != null, "商城ID不存在...");
		Shop shop = shopService.find(shopRequest.getId());
		Preconditions.checkArgument(shop != null, "商城不存在...");
		shop.setLogo(shopRequest.getLogo());
		return shopService.saveShop(shop);
	}
	
	@DeleteMapping("/{shopId}")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean deleteShop(@PathVariable("shopId") Long shopId) {
		shopService.delete(shopId);
		return Boolean.TRUE ; 
	}
	
	@GetMapping("/{shopId}/nav")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public List<Navigation> findShopNavigation(@PathVariable("shopId") Long shopId){
		return navigationService.findShopNavigation(shopId);
	}
	
	@PostMapping("/{shopId}/nav")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Navigation presistNavigation(@PathVariable("shopId") Long shopId ,@RequestBody Navigation navigationRequest) {
		Preconditions.checkArgument(StrUtil.isNotBlank(navigationRequest.getName()), "名称不能为空...");
		if(navigationRequest.getPriority() == null) navigationRequest.setPriority(0);
		if(navigationRequest.getIsShow() == null) navigationRequest.setIsShow(Boolean.FALSE);
		navigationRequest.setShopId(shopId);
		return navigationService.save(navigationRequest);
	}
	
	@DeleteMapping("/nav/{navId}")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean deleteNavigation(@PathVariable("navId") Long navId) {
		return navigationService.deleteNavigation(navId);
	}
	
	/**
	 * 导航绑定集合
	 */
	@PostMapping("/nav/collection")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public NavigationCollection bindNavCollection(@RequestBody NavigationCollection navigationCollection) {
		return navigationService.bindNavCollection(navigationCollection) ;
	}
	@PostMapping("/nav/collections")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public List<NavigationCollection> bindNavCollections(@RequestBody List<NavigationCollection> navigationCollections) {
		return navigationService.bindNavCollections(navigationCollections) ;
	}
	/**
	 * 导航删除集合
	 */
	@DeleteMapping("/nav/collection/{navCollectionId}")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public Boolean deleteNavCollection(@PathVariable("navCollectionId") Long navCollectionId) {
		return navigationService.deleteNavCollection(navCollectionId) ;
	}
	
	/**
	 * 导航下所有集合
	 */
	@GetMapping("/nav/{navId}/collection")
	@PreAuthorize("hasAnyRoles('admin', 'shop', 'affiliate')")
	public List<Map<String , Object>> findNavCollection(@PathVariable("navId") Long navId) {
		return navigationService.findNavCollection(navId) ;
	}
	
	/**
	 * 查詢所有留言
	 */
	@PreAuthorize("hasAnyRoles('admin', 'shop')")
	@GetMapping("/feedback")
	public Page<FeedBackSearchResponse> findFeedBackSearchResponse(FeedBackSearchRequest feedBackSearchRequest, @PageableDefault(sort = {"s_feedback.id"}, direction = Sort.Direction.DESC)Pageable pageable){
		
		return shopService.findFeedBackSearchResponse(feedBackSearchRequest, pageable);
		
	}
}
